package com.tianma315.admin.material.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.material.VO.MaterialAndTemplateVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.material.domain.MaterialDO;
import com.tianma315.admin.material.service.MaterialService;

/**
 * 
 * <pre>
 * 原材料
 * </pre>
 * <small> 2019-05-25 15:23:18 | Aron</small>
 */
@Controller
@RequestMapping("/material/material")
public class MaterialController extends BaseController{
	@Autowired
	private MaterialService materialService;
	
	@GetMapping()
	@RequiresPermissions("material:material:material")
	String Material(){
	    return "material/material/material";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("material:material:material")
	public Result<Page<MaterialAndTemplateVO>> list(Integer pageNumber, Integer pageSize, MaterialDO materialDTO){
		// 查询列表数据
        Page<MaterialAndTemplateVO> page = materialService.getMaterialAndTemplateVOPage(pageNumber,pageSize,materialDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("material:material:add")
	String add(){
	    return "material/material/add";
	}

	@GetMapping("/edit/{materialId}")
	@RequiresPermissions("material:material:edit")
	String edit(@PathVariable("materialId") Integer materialId,Model model){
		MaterialDO material = materialService.selectById(materialId);
		model.addAttribute("material", material);
	    return "material/material/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("material:material:add")
	public Result<String> save( MaterialDO material){
		material.setCreateBy(getUserId());
		material.setCreateDate(Calendar.getInstance().getTime());
		if(materialService.addMaterialAndTemplate(material)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("material:material:edit")
	public Result<String>  update( MaterialDO material){
		materialService.updateById(material);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("material:material:remove")
	public Result<String>  remove( Integer materialId){
		if(materialService.deleteMaterial(materialId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("material:material:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] materialIds){
		for (int i = 0; i < materialIds.length; i++) {
			materialService.deleteMaterial(materialIds[i]);
		}
		return Result.ok();
	}

	/**
	 * 所有数据
	 */
	@GetMapping( "/getMaterialList")
	@ResponseBody
	public Result<List<MaterialDO>>  getMaterialList(MaterialDO materialDO){
		Wrapper<MaterialDO> wrapper = new EntityWrapper<>();
		wrapper.eq("state",0);
		wrapper.like("material_name",materialDO.getMaterialName());
		List<MaterialDO> materialDOList = materialService.selectList(wrapper);
		return Result.ok(materialDOList);
	}
	
}
